package com.ruoyi.project.cms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.framework.web.page.PageDomain;
import com.ruoyi.framework.web.page.TableSupport;
import com.ruoyi.project.system.domain.DivisionDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.jetbrains.annotations.NotNull;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.cms.domain.DivisionLevel5;
import com.ruoyi.project.cms.service.IDivisionLevel5Service;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 行政区划Controller
 *
 * @author ruoyi
 * @date 2023-03-03
 */
@RestController
@RequestMapping("/cms/level5")
@Api("五级行政区划")
public class DivisionLevel5Controller extends BaseController {
    @Autowired
    private IDivisionLevel5Service divisionLevel5Service;

    /**
     * 查询行政区划列表
     */
    @PreAuthorize("@ss.hasPermi('cms:level5:list')")
    @GetMapping("/list")
    public TableDataInfo list(DivisionLevel5 divisionLevel5) {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        Map<String, Object> hashMap = new HashMap<String, Object>();
        hashMap.put("pageNum", pageNum);
        hashMap.put("pageSize", pageSize);
        divisionLevel5.setParams(hashMap);
        Page<DivisionLevel5> list = divisionLevel5Service.selectDivisionLevel5List(divisionLevel5);
        return getDataTable(list);
    }

    /**
     * 查询行政区划列表(复杂查询)
     */
    // @PreAuthorize("@ss.hasPermi('cms:level5:list')")
    @GetMapping("/list2")
    @ApiOperation("获取用户详细")
    @ApiImplicitParam(name = "divisionDto", value = "复杂查询Dto", required = true, dataTypeClass = DivisionDto.class)
    public TableDataInfo list2(@NotNull DivisionDto divisionDto) {
//        PageDomain pageDomain = TableSupport.buildPageRequest();
//        Integer pageNum = pageDomain.getPageNum();
//        Integer pageSize = pageDomain.getPageSize();
        TableDataInfo tableDataInfo = new TableDataInfo();
        if (divisionDto.getPageNum() == null || divisionDto.getPageSize() == null) {
            tableDataInfo.setMsg("分页相关参数不能为空");
            tableDataInfo.setCode(HttpStatus.ERROR);
            return tableDataInfo;
        }
        if (StrUtil.isEmpty(divisionDto.getIncludeValue())
                && StrUtil.isEmpty(divisionDto.getNotIncludeValue())) {
            tableDataInfo.setMsg("搜索关键词不能全为空");
            tableDataInfo.setCode(HttpStatus.ERROR);
            return tableDataInfo;
        }


        /**
         * 复杂查询条件
         * 1.包含
         * 2.不包含
         * 3.等级 1-5 省市区乡镇街道
         */
        String includeValue = divisionDto.getIncludeValue(); //包括
        String notIncludeValue = divisionDto.getNotIncludeValue();//不包括
        Integer level = null;
        LambdaQueryWrapper<DivisionLevel5> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.
                select(DivisionLevel5::getProvinceName,
                        DivisionLevel5::getCityName,
                        DivisionLevel5::getAreaName,
                        DivisionLevel5::getStreetName,
                        DivisionLevel5::getVillageName,
                        DivisionLevel5::getCode)
                .func(wrapper -> {
                            if (StrUtil.isNotEmpty(includeValue)) {
                                wrapper.and(wrapper1-> {
                                    wrapper1.like(DivisionLevel5::getProvinceName, includeValue).or()
                                            .like(DivisionLevel5::getCityName, includeValue).or()
                                            .like(DivisionLevel5::getAreaName, includeValue).or()
                                            .like(DivisionLevel5::getStreetName, includeValue).or()
                                            .like(DivisionLevel5::getVillageName, includeValue);

                                });
                            }
                        }
                ).func(
                wrapper -> {
                    if (StrUtil.isNotEmpty(notIncludeValue)) {
                        wrapper.and(wrapper1->{
                            wrapper1.notLike(DivisionLevel5::getProvinceName, notIncludeValue)
                                    .notLike(DivisionLevel5::getCityName, notIncludeValue)
                                    .notLike(DivisionLevel5::getAreaName, notIncludeValue)
                                    .notLike(DivisionLevel5::getStreetName, notIncludeValue)
                                    .notLike(DivisionLevel5::getVillageName, notIncludeValue);
                        });

                    }
                }
        ).orderByAsc(DivisionLevel5::getCode);
        Page<DivisionLevel5> page = new Page<>(divisionDto.getPageNum(), divisionDto.getPageSize());
        Page<DivisionLevel5> list = divisionLevel5Service.page(page, queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出行政区划列表
     */
    @PreAuthorize("@ss.hasPermi('cms:level5:export')")
    @Log(title = "行政区划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DivisionLevel5 divisionLevel5) {
        //List<DivisionLevel5> list = divisionLevel5Service.selectDivisionLevel5List(divisionLevel5);
        //ExcelUtil<DivisionLevel5> util = new ExcelUtil<DivisionLevel5>(DivisionLevel5.class);
        // util.exportExcel(response, list, "行政区划数据");
    }

    /**
     * 获取行政区划详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:level5:query')")
    @GetMapping(value = "/{provinceName}")
    public AjaxResult getInfo(@PathVariable("provinceName") String provinceName) {
        return AjaxResult.success(divisionLevel5Service.selectDivisionLevel5ByProvinceName(provinceName));
    }

    /**
     * 新增行政区划
     */
    @PreAuthorize("@ss.hasPermi('cms:level5:add')")
    @Log(title = "行政区划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DivisionLevel5 divisionLevel5) {
        return toAjax(divisionLevel5Service.insertDivisionLevel5(divisionLevel5));
    }

    /**
     * 修改行政区划
     */
    @PreAuthorize("@ss.hasPermi('cms:level5:edit')")
    @Log(title = "行政区划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DivisionLevel5 divisionLevel5) {
        return toAjax(divisionLevel5Service.updateDivisionLevel5(divisionLevel5));
    }

    /**
     * 删除行政区划
     */
    @PreAuthorize("@ss.hasPermi('cms:level5:remove')")
    @Log(title = "行政区划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{provinceNames}")
    public AjaxResult remove(@PathVariable String[] provinceNames) {
        return toAjax(divisionLevel5Service.deleteDivisionLevel5ByProvinceNames(provinceNames));
    }

    /**
     *
     */
    public AjaxResult tree(String parentId) {
        if (StrUtil.isEmpty(parentId)) {
            //查找省份
        } else {
            //子集
        }
        return null;
    }
}
